//
//  OMTBaseTableViewController.m
//  OMTUIKit
//
//  Created by 昭荣伊 on 2018/7/30.
//

#import "OMTBaseTableViewController.h"
#import <OMTCoreKit/OMTCoreKit.h>

// 默认重用ID
static NSString *const kDefaultReuseId = @"defaultCell";

@interface OMTBaseTableViewController ()

@property (nonatomic, assign) UITableViewStyle tableViewStyle;

@end

@implementation OMTBaseTableViewController

#pragma mark - LifeCircle
- (instancetype)initWithStyle:(UITableViewStyle)style {
    if (self = [super init]) {
        _tableViewStyle = style;
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        _tableViewStyle = UITableViewStylePlain;
    }
    return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        _tableViewStyle = UITableViewStylePlain;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self setupBaseTableViewSubviews];
    [self setupBaseTableViewConstraints];
}

- (void)setupBaseTableViewSubviews {
    [self.view addSubview:self.tableView];
}

- (void)setupBaseTableViewConstraints {
    self.tableView.top = self.omt_navigationBar.bottom;
    self.tableView.height = self.view.height - self.tableView.top;
    self.tableView.left = 0;
    self.tableView.width = self.view.width;
}

#pragma mark - OMTAutoHideKeyboardable
- (BOOL)whenScrollTableViewHideKeyboard {
    return YES;
}

#pragma mark - OMTStatusViewDelegate
- (UIView *)statusViewSuperView {
    return self.tableView;
}

#pragma mark - OMTRefreshable

#if 0
- (void)refresh {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
}

- (void)fetchMore {
    [self.tableView.mj_footer endRefreshing];
}
#endif


#pragma mark - UIScrollViewDatasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01F;;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01F;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDefaultReuseId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kDefaultReuseId];
    }
    return cell;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if ([self whenScrollTableViewHideKeyboard]) {
        [self.view endEditing:YES];
    }
}

#pragma mark - UI Lazyload

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:self.tableViewStyle];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        _tableView.backgroundColor = [UIColor clearColor];
        // 关闭行高估算
        _tableView.estimatedRowHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        
        // 刷新
        if ([self respondsToSelector:@selector(refresh)]) {
            NSString *headerClassName = OMTBaseTableViewController.headerClassName;
            if (!headerClassName) {
                headerClassName = @"MJRefreshNormalHeader";
            }
            Class header = NSClassFromString(headerClassName);
            _tableView.mj_header = [header headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
        }
        
        // 加载更多
        if ([self respondsToSelector:@selector(fetchMore)]) {
            NSString *footerClassName = OMTBaseTableViewController.footerClassName;
            if (!footerClassName) {
                footerClassName = @"MJRefreshAutoFooter";
            }
            Class footer = NSClassFromString(footerClassName);
            _tableView.mj_footer = [footer footerWithRefreshingTarget:self refreshingAction:@selector(fetchMore)];
            // 加载更多默认为没更多数据
            _tableView.mj_footer.state = MJRefreshStateNoMoreData;
            _tableView.mj_footer.hidden = YES;
        }
    }
    return _tableView;
}

#pragma mark - Getters And Setters
+ (void)setHeaderClassName:(NSString *)headerClassName {
    objc_setAssociatedObject(self, @selector(headerClassName), headerClassName, OBJC_ASSOCIATION_COPY);
}

+ (NSString *)headerClassName {
    return objc_getAssociatedObject(self, @selector(headerClassName));
}

+ (void)setFooterClassName:(NSString *)footerClassName{
    objc_setAssociatedObject(self, @selector(footerClassName), footerClassName, OBJC_ASSOCIATION_COPY);
}

+ (NSString *)footerClassName {
    return objc_getAssociatedObject(self, @selector(footerClassName));
}


@end
